import * as dd from 'dingtalk-jsapi'
import {Notify} from 'vant'

export default async function getCode() {
    let code = ''
    await new Promise((resolve) => {
        if (dd.env.platform !== 'notInDingTalk') {
            //进行钉钉登录操作
            dd.ready(function () {
                // 拉取微应用免登授权码
                dd.runtime.permission.requestAuthCode({
                    corpId: process.env.VUE_APP_DD_CORPID, // 企业id
                    onSuccess: function (result) {
                        code = result.code
                        resolve()
                    },
                    onFail: function (err) {
                        alert(JSON.stringify(err));
                        // Notify({type: 'danger', message: '未获取到钉钉权限，请返回重试！', duration: 0})
                        resolve()
                    }
                })
            })
        } else {
            Notify({type: 'danger', message: '不是钉钉运行环境，请返回重试！', duration: 0})
            resolve()
        }
    })
    return code
}